package com.hyj.jpdy.job.RunModle;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.hyj.jpdy.bean.vo.Article;
import com.hyj.jpdy.grap.html.GrapHtmlUtil;
import com.hyj.jpdy.grap.html.JokeGrap;
import com.hyj.jpdy.grap.html.bean.HtmlInfoVO;
import com.hyj.jpdy.grap.html.bean.HtmlQO;
import com.hyj.jpdy.service.IGrapService;

public class SinaModle implements Runnable {

	private IGrapService grapService;
	Log log = LogFactory.getLog(SinaModle.class);
	
	public SinaModle(IGrapService grapService){
		this.grapService = grapService;
	}
	
	@Override
	public void run() {
		 String url = "http://www.sina.com.cn/";
		 Document doc = GrapHtmlUtil.getDocument(url,"GBK");
		 if(doc !=null){
			 // 首页最热门新闻
			 Element content = doc.getElementById("syncad_0");
			 Elements links = content.getElementsByTag("a");
			 for (Element link : links) {  
					String linkHref = link.attr("href");
					String linkText = link.text();
					Article item = new Article();
					item.setFromUrl(linkHref);
					item.setTitle(linkText);
				    item.setColumnName("新浪新闻");
					String contentId ="artibody";
					 
					HtmlQO qo = new HtmlQO();
					qo.setCharset("GBK");
					qo.setUrl(linkHref);
					qo.setContentId(contentId);
				 
					HtmlInfoVO vo = GrapHtmlUtil.getContent(qo);
					if(vo != null){
					if(vo.getHtmlContent()== null || vo.getHtmlContent().equals("")){
						continue;
					}
					item.setContent(vo.getHtmlContent());
					 try{ 
						   item.setTypeId(999);
						   grapService.saveArticle(item);
					    }catch(Exception e){
					       e.printStackTrace();
					    }
					}
					
					
			 }
	}

	}

}
